package com.medvalley.tcm.service.impl;

import com.google.common.collect.Lists;
import com.medvalley.tcm.dao.DepartmentDao;
import com.medvalley.tcm.dao.DoctorPositionDao;
import com.medvalley.tcm.dao.OrgDao;
import com.medvalley.tcm.domain.Department;
import com.medvalley.tcm.domain.DoctorPosition;
import com.medvalley.tcm.domain.Organization;
import com.medvalley.tcm.service.DictionaryService;
import com.medvalley.tcm.vo.outputvo.OrgOutputVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author linsong
 * @date 2019/5/7 13:54
 **/
@Service
public class DictionaryServiceImpl implements DictionaryService, InitializingBean {

    @Autowired
    private DoctorPositionDao doctorPositionDao;

    @Autowired
    private OrgDao orgDao;

    @Autowired
    private DepartmentDao departmentDao;

    private static final Map<Integer, String> POSITION_MAP = new HashMap<>();
    private static final Map<String, String> DEPARTMENT_MAP = new HashMap<>();

    @Override
    public List allDoctorPosition() {
        return Lists.newArrayList(doctorPositionDao.findAll());
    }

    @Override
    public void addOrg(String orgName) {
        Organization organization = new Organization();
        organization.setName(orgName);
        orgDao.save(organization);
    }

    @Override
    public List listOrg() {
        List<Organization> organizationList = Lists.newArrayList(orgDao.findAll());
        List<OrgOutputVo> outputVo = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(organizationList)) {
            organizationList.forEach(o -> {
                OrgOutputVo orgOutputVo = new OrgOutputVo();
                orgOutputVo.setId(o.getId());
                orgOutputVo.setName(o.getName());
                outputVo.add(orgOutputVo);
            });
        }
        return outputVo;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        initPosition();
        initDepartment();
    }

    private void initPosition() {
        Iterable<DoctorPosition> positions = doctorPositionDao.findAll();
        Iterator<DoctorPosition> iterator = positions.iterator();
        while (iterator.hasNext()) {
            DoctorPosition dp = iterator.next();
            POSITION_MAP.put(dp.getId(), dp.getName());
        }
    }

    private void initDepartment() {
        Iterable<Department> departments = departmentDao.findAll();
        Iterator<Department> departmentIterator = departments.iterator();
        while (departmentIterator.hasNext()) {
            Department dp = departmentIterator.next();
            DEPARTMENT_MAP.put(dp.getDNo(), dp.getDName());
        }
    }

    public static String getPositionName(int positionId) {
        return POSITION_MAP.get(positionId);
    }

    public static String getDepartmentName(String departmentNo) {
        return DEPARTMENT_MAP.get(departmentNo);
    }

}
